package cn.wolfcode.web.controller;

import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.PermissionService;
import cn.wolfcode.vo.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 权限处理器
 *
 * @author 杨兴健
 * @Date 2020/7/9 10:30
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService service;

    /**
     * 查询所有权限
     *
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    @RequiresPermissions(value = {"permission:list", "权限列表"}, logical = Logical.OR)
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {

        model.addAttribute("pageInfo", service.selectAllByQO(qo));
        return "/permission/list";
    }

    /**
     * 根据id删除权限
     *
     * @param pid
     * @return
     */
    @RequestMapping("/delete")
    @RequiresPermissions(value = {"permission:delete", "权限删除"}, logical = Logical.OR)
    public String delete(Long pid) {
        service.delete(pid);
        return "redirect:/permission/list";
    }

    /**
     * 更新权限
     *
     * @return
     */
    @RequestMapping("/reload")
    @ResponseBody
    @RequiresPermissions(value = {"permission:reload", "权限更新"}, logical = Logical.OR)
    public JsonResult reload() {

        service.reload();
        return new JsonResult(true, "更新成功！");

    }

    @RequestMapping("/toNoPermission")
    public String toNoPermission() {
        return "common/nopermission";
    }
}
